package com.example.demo.hotel.service;

import com.example.demo.hotel.entity.RoomType;
import com.example.demo.hotel.repository.RoomTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoomTypeService {

    @Autowired
    private RoomTypeRepository roomTypeRepository;

    public Page<RoomType> getRoomTypes(int page, int size) {
        return roomTypeRepository.findAll(PageRequest.of(page, size));
    }

    @Transactional
    public RoomType createRoomType(RoomType roomType) {
        return roomTypeRepository.save(roomType);
    }

    @Transactional
    public RoomType updateRoomType(Long id, RoomType roomType) {
        RoomType existingRoomType = roomTypeRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Room type not found"));
        
        existingRoomType.setName(roomType.getName());
        existingRoomType.setPrice(roomType.getPrice());
        existingRoomType.setArea(roomType.getArea());
        existingRoomType.setBedType(roomType.getBedType());
        existingRoomType.setCapacity(roomType.getCapacity());
        existingRoomType.setStatus(roomType.getStatus());
        
        return roomTypeRepository.save(existingRoomType);
    }

    @Transactional
    public void deleteRoomType(Long id) {
        roomTypeRepository.deleteById(id);
    }

    public RoomType getRoomTypeById(Long id) {
        return roomTypeRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Room type not found"));
    }
}
